#region license

// Copyright (c) 2008, Rinat Abdullin (http://rabdullin.com)
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
// 
//     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
//     * Neither the name of the Rinat Abdullin nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#endregion

using System.Collections.Generic;
using Boo.Lang.Compiler.Ast;
using Boo.Lang.Compiler.Steps;

namespace Photon.Interception
{
	/// <summary>
	/// This compiler step turns "throw new NotImplementedException" stubs into
	/// the calls to the _inner
	/// </summary>
	internal sealed class FixStubs : AbstractCompilerStep
	{
		public override void Run()
		{
			foreach (var module in CompileUnit.Modules)
			{
				foreach (var member in module.Members)
				{
					var classDef = member as ClassDefinition;

					if ((classDef != null) && (classDef.IsIntercepted()))
					{
						FixMembers(classDef.Members);
					}
				}
			}
		}

		private static void FixMembers(IEnumerable<TypeMember> members)
		{
			foreach (var member in members)
			{
				if (member.IsIntercepted())
				{
					continue;
				}
				if (member is Method)
				{
					FixMethod(member as Method);
				}
			}
		}

		private static void FixMethod(Method method)
		{
			var statements = method.Body.Statements;
			if ((statements.Count != 1) || !(statements[0] is RaiseStatement))
			{
				return;
			}

			var target = new ReferenceExpression(PhotonSetup.InnerFieldName);
			var methodRef = new MemberReferenceExpression(target, method.Name);
			var call = new MethodInvocationExpression(methodRef);

			foreach (var parameter in method.Parameters)
			{
				var param = new ReferenceExpression(parameter.Name);
				call.Arguments.Add(param);
			}
			method.Body = new Block(LexicalInfo.Empty);
			if (method.ReturnType.ToString() != "void")
			{
				method.Body.Add(new ReturnStatement(call));
			}
			else
			{
				method.Body.Add(call);
			}
		}
	}
}